package com.gm.wj.dao;

import com.gm.wj.entity.CourseCategory;
import com.gm.wj.entity.SysCourse;
import org.hibernate.annotations.Where;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: wj
 * @description:
 * @author: Huang_shengjun
 * @create: 2020-08-31 15:20
 **/
public interface SysCourseDAO extends JpaRepository<SysCourse,Integer> {
    SysCourse findById(int id);

    @Query(value = "select * from sys_course  where display = ?1 and is_delete = 0",nativeQuery = true)
    List findByDisplay(int i);

    // 多条件模糊查询
    @Query(value = "select * from sys_course s where  (?1 is null or s.course_name like ?1) and (?2 is null or s.teacher like ?2) and (?3 is null or s.display = ?3) and is_delete = 0",nativeQuery = true)
    List<SysCourse> find(String queryKey1, String queryKey2, String queryKey3);

    // 上架查询
    @Query(value = "select * from sys_course  where display = 1 and is_delete = 0 ORDER BY id DESC;",nativeQuery = true)
    List<SysCourse> findByDisplay();

    SysCourse findByCourseId(String courseId);

    // 开团，扣减库存
    @Transactional
    @Modifying
    @Query(value = "update sys_course set num = num - ?2 where course_id=?1 ", nativeQuery = true)
    void deductionStock(String courseId, int i);

    // 拼团失败，归还库存
    @Transactional
    @Modifying
    @Query(value = "update sys_course set num = num + ?2 where course_id=?1 ", nativeQuery = true)
    void releaseStock(String courseId, int i);

    @Query(value = "select * from sys_course  where course_type = ?1 and display = 1 and is_delete = 0 ORDER BY id ASC;",nativeQuery = true)
    List<SysCourse> findAllByCourseType(int cid);

    @Query(value = "select * from sys_course  where course_name like ?1 or teacher like ?2 and (display = 1 and is_delete = 0) ORDER BY id DESC;",nativeQuery = true)
    List<SysCourse> findAllByCourseNameOrTeacher(String s, String s1);

    Page<SysCourse> findAll(Specification<SysCourse> sysCourseSpecification, Pageable pageable);
}